home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Dave Mark / Mac C Primer 1 (SC++7) / 4.1 - EventTracker / EventTracker.c next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  6.8 KB  |  301 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  EventTracker Code from Chapter Four of                 */
  4. /*                                                        */
  5. /*    ** The Macintosh C Programming Primer, 2nd Ed. **    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /*  This program demonstrates specific Mac programming    */
  10. /*    techniques.                                            */
  11. /*                                                        */    
  12. /********************************************************/
  13.  
  14. #include <AppleEvents.h>
  15. #include <GestaltEqu.h>
  16. #include <limits.h>
  17.  
  18. #define kBaseResID            128
  19. #define    kMoveToFront        (WindowPtr)-1L
  20. #define kSleep                LONG_MAX
  21.  
  22. #define    kLeftMargin            4
  23. #define kRowStart            285
  24. #define    kFontSize            9
  25. #define    kRowHeight            (kFontSize + 2)
  26. #define    kHorizontalOffset    0
  27.  
  28. #define kGestaltMask        1L
  29.  
  30. /*************/
  31. /*  Globals  */
  32. /*************/
  33.  
  34. Boolean                gDone;
  35. AEEventHandlerUPP    gDoOpenAppUPP,
  36.                     gDoOpenDocUPP,
  37.                     gDoPrintDocUPP,
  38.                     gDoQuitAppUPP;
  39.  
  40. /***************/
  41. /*  Functions  */
  42. /***************/
  43.  
  44. void            ToolBoxInit( void );
  45. void            WindowInit( void );
  46. void            EventInit( void );
  47. void            EventLoop( void );
  48. void            DoEvent( EventRecord *eventPtr );
  49. pascal OSErr    DoOpenApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon  );
  50. pascal OSErr    DoOpenDoc( AppleEvent theAppleEvent, AppleEvent reply, long refCon  );
  51. pascal OSErr    DoPrintDoc( AppleEvent theAppleEvent, AppleEvent reply, long refCon );
  52. pascal OSErr    DoQuitApp( AppleEvent theAppleEvent, AppleEvent reply, long refCon  );
  53. void            DrawEventString( Str255 eventString );
  54. void            HandleMouseDown( EventRecord *eventPtr );
  55.  
  56.  
  57. /******************************** main *********/
  58.  
  59. void    main( void )
  60. {
  61.     ToolBoxInit();
  62.     WindowInit();
  63.     EventInit();
  64.     
  65.     EventLoop();
  66. }
  67.  
  68.  
  69. /*********************************** ToolBoxInit */
  70.  
  71. void    ToolBoxInit( void )
  72. {
  73.     InitGraf( &qd.thePort );
  74.     InitFonts();
  75.     InitWindows();
  76.     InitMenus();
  77.     TEInit();
  78.     InitDialogs( 0L );
  79.     InitCursor();
  80. }
  81.  
  82.  
  83. /******************************** WindowInit *********/
  84.  
  85. void    WindowInit( void )
  86. {
  87.     WindowPtr    window;
  88.     
  89.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  90.     
  91.     if ( window == nil )
  92.     {
  93.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  94.         ExitToShell();
  95.     }
  96.     
  97.     SetPort( window );
  98.     TextSize( kFontSize );
  99.     
  100.     ShowWindow( window );
  101. }
  102.  
  103.  
  104. /******************************** EventInit *********/
  105.  
  106. void    EventInit( void )
  107. {
  108.     OSErr    err;
  109.     long    feature;
  110.     
  111.     err = Gestalt( gestaltAppleEventsAttr, &feature );
  112.     
  113.     if ( err != noErr )
  114.     {
  115.         DrawEventString( "\pproblem in calling Gestalt!" );
  116.         return;
  117.     }
  118.     else
  119.     {
  120.         if ( !( feature & ( kGestaltMask << gestaltAppleEventsPresent ) ) )
  121.         {
  122.             DrawEventString( "\pApple events not available!" );
  123.             return;
  124.         }
  125.     }
  126.     
  127.     gDoOpenAppUPP = NewAEEventHandlerProc( DoOpenApp );
  128.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  129.                 gDoOpenAppUPP, 0L, false );
  130.     if ( err != noErr ) DrawEventString( "\pkAEOpenApplication Apple event not available!" );
  131.     
  132.     gDoOpenDocUPP = NewAEEventHandlerProc( DoOpenDoc );
  133.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  134.                 gDoOpenDocUPP, 0L, false );
  135.     if ( err != noErr ) DrawEventString( "\pkAEOpenDocuments Apple event not available!" );
  136.                 
  137.     gDoPrintDocUPP = NewAEEventHandlerProc( DoPrintDoc );
  138.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  139.                 gDoPrintDocUPP, 0L, false );
  140.     if ( err != noErr ) DrawEventString( "\pkAEPrintDocuments Apple event not available!" );
  141.     
  142.     gDoQuitAppUPP = NewAEEventHandlerProc( DoQuitApp );
  143.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  144.                 gDoQuitAppUPP, 0L, false );
  145.     if ( err != noErr ) DrawEventString( "\pkAEQuitApplication Apple event not available!" );
  146. }
  147.  
  148.  
  149. /******************************** EventLoop *********/
  150.  
  151. void    EventLoop( void )
  152. {        
  153.     EventRecord        event;
  154.     
  155.     gDone = false;
  156.     while ( gDone == false )
  157.     {
  158.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  159.             DoEvent( &event );
  160.         /*else DrawEventString( "\pnullEvent" );*/
  161.         /*    Uncomment the previous line for a burst of flavor!  */
  162.     }
  163. }
  164.  
  165.  
  166. /************************************* DoEvent     */
  167.  
  168. void    DoEvent( EventRecord *eventPtr )
  169. {
  170.     switch ( eventPtr->what )
  171.     {
  172.         case kHighLevelEvent:
  173.             DrawEventString( "\pHigh level event: " );
  174.             AEProcessAppleEvent( eventPtr );
  175.             break;
  176.         case mouseDown: 
  177.             DrawEventString( "\pmouseDown" );
  178.             HandleMouseDown( eventPtr );
  179.             break;
  180.         case mouseUp: 
  181.             DrawEventString( "\pmouseUp" );
  182.             break;
  183.         case keyDown:
  184.             DrawEventString( "\pkeyDown" );
  185.             break;
  186.         case keyUp:
  187.             DrawEventString( "\pkeyUp" );
  188.             break;
  189.         case autoKey:
  190.             DrawEventString( "\pautoKey" );
  191.             break;
  192.         case updateEvt:
  193.             DrawEventString( "\pupdateEvt" );
  194.             BeginUpdate( (WindowPtr)eventPtr->message );
  195.             EndUpdate( (WindowPtr)eventPtr->message );
  196.             break;
  197.         case diskEvt:
  198.             DrawEventString( "\pdiskEvt" );
  199.             break;
  200.         case activateEvt:
  201.             DrawEventString( "\pactivateEvt" );
  202.             break;
  203.         case networkEvt:
  204.             DrawEventString( "\pnetworkEvt" );
  205.             break;
  206.         case driverEvt:
  207.             DrawEventString( "\pdriverEvt" );
  208.             break;
  209.         case app1Evt:
  210.             DrawEventString( "\papp1Evt" );
  211.             break;
  212.         case app2Evt:
  213.             DrawEventString( "\papp2Evt" );
  214.             break;
  215.         case app3Evt:
  216.             DrawEventString( "\papp3Evt" );
  217.             break;
  218.         case osEvt:
  219.             DrawEventString( "\posEvt: " );
  220.             if ( ( eventPtr->message & suspendResumeMessage ) == resumeFlag )
  221.                 DrawString( "\pResume event" );
  222.             else
  223.                 DrawString( "\pSuspend event" );
  224.             break;
  225.     }
  226. }
  227.  
  228. /************************************* DoOpenApp     */
  229.  
  230. pascal OSErr    DoOpenApp( AppleEvent theAppleEvent, AppleEvent reply,
  231.             long refCon )
  232. {
  233.     DrawString( "\pApple event: kAEOpenApplication" );
  234. }
  235.  
  236.  
  237. /************************************* DoOpenDoc     */
  238.  
  239. pascal OSErr    DoOpenDoc( AppleEvent theAppleEvent, AppleEvent reply,
  240.             long refCon )
  241. {
  242.     DrawString( "\pApple event: kAEOpenDocuments" );
  243. }
  244.  
  245.  
  246. /************************************* DoPrintDoc     */
  247.  
  248. pascal OSErr    DoPrintDoc( AppleEvent theAppleEvent, AppleEvent reply,
  249.             long refCon )
  250. {
  251.     DrawString( "\pApple event: kAEPrintDocuments" );
  252. }
  253.  
  254.  
  255. /************************************* DoQuitApp     */
  256.  
  257. pascal OSErr    DoQuitApp( AppleEvent theAppleEvent, AppleEvent reply,
  258.             long refCon )
  259. {
  260.     DrawString( "\pApple event: kAEQuitApplication" );
  261. }
  262.  
  263. /************************************* DrawEventString *******/
  264.  
  265. void    DrawEventString( Str255 eventString )
  266. {
  267.     RgnHandle    tempRgn;
  268.     WindowPtr    window;
  269.     
  270.     window = FrontWindow();
  271.     tempRgn = NewRgn();
  272.     ScrollRect( &window->portRect, kHorizontalOffset, -kRowHeight, tempRgn );
  273.     DisposeRgn( tempRgn );
  274.  
  275.     MoveTo( kLeftMargin, kRowStart );
  276.     DrawString( eventString );
  277. }
  278.  
  279.  
  280. /************************************* HandleMouseDown */
  281.  
  282. void    HandleMouseDown( EventRecord *eventPtr )
  283. {
  284.     WindowPtr    window;
  285.     long        thePart;
  286.     
  287.     thePart = FindWindow( eventPtr->where, &window );
  288.     
  289.     switch ( thePart )
  290.     {
  291.         case inSysWindow : 
  292.             SystemClick( eventPtr, window );
  293.             break;
  294.         case inDrag : 
  295.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  296.             break;
  297.         case inGoAway : 
  298.             gDone = true;
  299.             break;
  300.     }
  301. }